import Sqlite from 'react-native-sqlite-storage';
let db = null;
let DBNAME = "fetail.sqlite3";

export function getConnect(){
    if(!db || db.openDBs[DBNAME] != "OPEN"){
        db = Sqlite.openDatabase({"name":DBNAME,createFromLocation:1},(mdb)=>{
            console.log("sqlite 准备就绪");
        },()=>{
            console.warn("sqlite 链接失败！");
        });
    }
    return db;
}

export function closeConnect(){
    if(db && db.openDBs[DBNAME] == "OPEN"){
        db.close();
    }
}

export function excute(sql, params = []) {
    return new Promise((resolve,reject)=>{
        let mdb = getConnect();
        if(mdb){
            mdb.executeSql(sql,params,(res)=>{
                resolve(res);
            },(e)=>{
                reject(e);
            });
        }else{
            reject(e);
        }
    });
}

export default getConnect();
